﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Linq;

public class HandBook : MonoBehaviour
{
    [SerializeField] Image imgTransform_True;
    [SerializeField] Transform imgTransform_Game;
    [SerializeField] TextMeshProUGUI name,text,haveNum;
    [SerializeField] string bName = "";

    private static readonly string gameImgPath = @"Bird/Game/";
    private static readonly string trueImgPath = @"Bird/True/";

    private string nowName;
    private GameObject lastImg;

    private void Start()
    {
        if (bName!="")
        {
            Init(bName);
        }
    }

    public void Init(string bName)
    {
        if (Bird_Data.birdDic_simpleName.ContainsKey(bName))
        {
            this.bName = bName;
            nowName = bName;
            var b = Bird_Data.birdDic_simpleName[bName];
            imgTransform_True.sprite = Resources.Load<Sprite>(trueImgPath+ b.img_True);

            var imgg = Resources.Load<GameObject>(gameImgPath+b.img_Game);
            if (lastImg!=null)
            {
                Destroy(lastImg);
            }
            var imggObj = Instantiate<GameObject>(imgg, imgTransform_Game);
            lastImg = imggObj;

            name.text = Language.Get(b.t_Name);
            text.text = Language.Get(b.t_Text);
        }
        haveNum.text = Language.Get("已解锁: ") + SaveData._.BirdInfo.Values.Where(b=>b).Count() + "/10";
    }

    public void Next()
    {
        var list = SaveData._.BirdInfo.Keys.ToList();
        int index = list.IndexOf(nowName);
        bool inWhile = true;
        do
        {
            index++;
            if (index >= list.Count)
            {
                index = 0;
            }

            if (SaveData._.BirdInfo[list[index]])
            {
                Init(list[index]);
                return;
            }
            
        } while (inWhile);
    }

    public void Per()
    {
        var list = SaveData._.BirdInfo.Keys.ToList();
        int index = list.IndexOf(nowName);
        bool inWhile = true;
        do
        {
            index--;
            if (index<0)
            {
                index = list.Count-1;
            }
            if (SaveData._.BirdInfo[list[index]])
            {
                Init(list[index]);
                return;
            }

        } while (inWhile);
    }

    public void Exit()
    {
        Destroy(gameObject);
    }
}
